package edu.hebeu.evil.freshmenguide.Activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.view.SubMenu;

import com.hyman.treemenu.DrawerMenuLayout;
import com.hyman.treemenu.MenuView;
import com.hyman.treemenu.pojo.SecMenu;
import com.hyman.treemenu.pojo.TopMenu;
import com.mylhyl.acp.Acp;
import com.mylhyl.acp.AcpListener;
import com.mylhyl.acp.AcpOptions;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import edu.hebeu.evil.freshmenguide.R;
import edu.hebeu.evil.freshmenguide.Utils.Utils;

/********************************************
 * 工程名：FreshmenGuide
 * 包名：edu.hebeu.evil.freshmenguide.Activity
 * 时间：16-7-20 上午8:13
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：
 ********************************************/
public class AddressListActivity extends BaseActivity
{
    private DrawerMenuLayout drawerMenuLayout;

    @Override
    public int setContentView()
    {
        return R.layout.addresslist;
    }

    @Override
    public String setToolbarTitle()
    {
        return "办公室联系电话";
    }

    @Override
    public void onCreate()
    {
        initView();
        setDrawerMenuLayout();
    }

    /**
     * 初始化控件
     */
    private void initView()
    {
        drawerMenuLayout = (DrawerMenuLayout) findViewById(R.id.drawerMenuLayout);
    }

    /**
     * 初始化电话列表
     *
     * @return
     */
    private List<TopMenu> initMenuList()
    {
        List<TopMenu> topMenus = new LinkedList<>();

        {
            List<SecMenu> secMenus = new LinkedList<>();
            {
                SecMenu secMenu1 = new SecMenu("白雨浓 18832030507", null, null);
                secMenu1.setTag(11);
                secMenu1.leftIcon = Utils.getDrawable(R.drawable.telephone);
                secMenus.add(secMenu1);

                SecMenu secMenu2 = new SecMenu("白雨浓 18832030507");
                secMenu2.setTag(12);
                secMenu2.leftIcon = Utils.getDrawable(R.drawable.telephone);
                secMenus.add(secMenu2);
            }
            TopMenu topMenu1 = new TopMenu("列表1", secMenus);
            topMenu1.setTag(1);
            topMenu1.leftIcon = Utils.getDrawable(R.drawable.contacts);
            topMenus.add(topMenu1);

        }
        {
            TopMenu topMenu2 = new TopMenu("列表2");
            topMenu2.setTag(2);
            topMenu2.leftIcon = Utils.getDrawable(R.drawable.contacts);
            topMenus.add(topMenu2);
        }

        return topMenus;
    }

    /**
     * 设置联系电话列表
     */
    private void setDrawerMenuLayout()
    {
        drawerMenuLayout.createMenus(initMenuList());
        drawerMenuLayout.setOnMenuClickListener(new DrawerMenuLayout.OnMenuClickListener()
        {
            @Override
            public void onMenuClick(MenuView menu)
            {
                //Utils.Toast(menu.getTag());
                if ((int) menu.getTag() > 10)
                {
                    final String telephone = menu.getTitle().split(" ")[1];
                    callTelephone(telephone);
                }
            }
        });
    }

    /**
     * 拨打电话
     */
    private void callTelephone(final String telephone)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("是否拨打电话?");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialogInterface, int i)
            {
                Acp.getInstance(AddressListActivity.this)
                        .request(new AcpOptions.Builder()
                                        .setPermissions(new String[]{Manifest.permission.CALL_PHONE}).build()
                                , new AcpListener()
                                {
                                    @Override
                                    public void onGranted()
                                    {
                                        if (ActivityCompat.checkSelfPermission(AddressListActivity.this
                                                , Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
                                        {
                                            Utils.Toast("权限获取失败");
                                            return;
                                        }
                                        startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telephone)));
                                    }

                                    @Override
                                    public void onDenied(List<String> permissions)
                                    {
                                    }
                                });
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialogInterface, int i)
            {
                dialogInterface.dismiss();
            }
        });

        builder.show();
    }
}
